#include <string>
#include <vector>
#include <unistd.h>
#include "BlockQueue.hpp"
#include "Thread.hpp"

using namespace ThreadModule;

int a = 10;
void Consumer(BlockQueue<int> &bq)
{
    while (true)
    {
        int data;
        bq.Pop(&data);
        cout << "consumer consum data:" << data << ",ID:" << &data << endl;
        sleep(1);
    }
}
void Productor(BlockQueue<int> &bq)
{
    int cnt = 1;
    while (true)
    {
        bq.Enqueue(cnt);
        cout << "Productor product data:" << cnt << ",ID:" << &cnt << endl;
        cnt++;
    }
}
void StartComm(vector<Thread<BlockQueue<int>>> &threads, int num, BlockQueue<int> &bq, func_t<BlockQueue<int>> func)
{
    for (int i = 0; i < num; i++)
    {
        string name = "thread-" + to_string(i + 1);
        threads.emplace_back(func, bq, name);
        threads.back().Start();
    }
}
void StartConsumer(std::vector<Thread<BlockQueue<int>>> &threads, int num, BlockQueue<int> &bq)
{
    StartComm(threads, num, bq, Consumer);
}

void StartProductor(std::vector<Thread<BlockQueue<int>>> &threads, int num, BlockQueue<int> &bq)
{
    StartComm(threads, num, bq, Productor);
}
void WaitAllThread(std::vector<Thread<BlockQueue<int>>> &threads)
{
    for (auto &thread : threads)
    {
        thread.Join();
    }
}

int main()
{
    BlockQueue<int> bq(5);
    vector<Thread<BlockQueue<int>>> threads;
    StartProductor(threads, 3, bq);
    StartConsumer(threads, 4, bq);
    WaitAllThread(threads);
    return 0;
}